package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.FastMail;



/**
 * @author daihu
 * @title : 快递网点操作实现
 */
@Service
public class FastMailService extends BaseDao<FastMail> {

	//@Autowired
	//private ConfService confService;

	/**
	 * 按条件查询
	 * @param fastmail
	 * @return
	 */
	public List<FastMail> query(FastMail fastmail) {
		if(fastmail == null) return super.findAll();
		//return super.find(createFilter(news, news.getiDisplayLength() > 0, date));
		if(fastmail.getiDisplayLength() > 0){
		  return super.find(createFilter(fastmail),fastmail.getiDisplayStart(),fastmail.getiDisplayLength());
		}
		return super.find(createFilter(fastmail));
	}

	public Long count(FastMail fastmail) {
		return super.count(createFilter(fastmail));
	}

	/**
	 * 拼装条件
	 * @param fastmail
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(FastMail fastmail) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(fastmail.getCompany())) {
      dc.add(Restrictions.like("company", fastmail.getCompany().trim(),MatchMode.ANYWHERE));
    }
	  if(!StringUtil.isNullOrEmpty(fastmail.getArea())) {
      dc.add(Restrictions.like("area", fastmail.getArea().trim(),MatchMode.ANYWHERE));
    }
	  if(!StringUtil.isNullOrEmpty(fastmail.getBrand())) {
      dc.add(Restrictions.like("brand", fastmail.getBrand().trim(),MatchMode.ANYWHERE));
    }
	  if(!StringUtil.isNullOrEmpty(fastmail.getSortType())) {
	    dc.addOrder(Order.desc("id"));
	  };
		return dc;
	}
}
